<?php

/**
 * 数据库基础操作类
 * Enter description here ...
 * @author fancy
 *
 */
class DBSQL {
	private $CONN = "";
	
	/**
	 * 初始化构造函数 连接数据库
	 * Enter description here ...
	 */
	public function __construct(){
		try {
			$conn = mysql_connect(ServerName,UserName,PassWord);//连接mysql数据库
		}catch (Exception $e){
			$msg = $e;
			error_log("__construct-error-msg : ".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		try {
			mysql_select_db(DBName,$conn);//选择数据库
			mysql_query("set names 'utf8'");
		} catch (Exception $e) {
			$msg = $e;
			error_log("__construct-error-msg2 :".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		
		$this->CONN = $conn;
	}
	
	/**
	 * 数据库查询函数
	 * Enter description here ...
	 * @param unknown_type $sql SQL语句
	 */
	public function select($sql=""){
		if(empty($sql))return false;
		if(empty($this->CONN))return false;
		try {
			mysql_query("set names 'utf8'");
			$results = mysql_query($sql,$this->CONN);
		} catch (Exception $e) {
			$msg = $e;
			error_log("select-error-msg :".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		if((!$results)or (empty($results))){//如果查询结果为空 返回false
			//mysql_free_result($results);
			return false;
		}
		$count = 0;
		$data = array();
		while($row = mysql_fetch_array($results)){
			$data[$count] = $row;
			$count++;
		}
		mysql_free_result($results);
		return $data;
	}
	
	/**
	 * 数据库 新增方法
	 * Enter description here ...
	 * @param unknown_type $sql
	 */
	public function insert($sql=""){
		if(empty($sql)) return 0;
		if(empty($this->CONN))return 0;
		try {
			$results = mysql_query($sql,$this->CONN);
		} catch (Exception $e) {
			$msg = $e;
			error_log("insert-error-msg :".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		if(!$results){
			return 0 ;
		}else{
			return @mysql_insert_id($this->CONN);
		}
	}
	
	/**
	 * 数据库 更新方法
	 * Enter description here ...
	 * @param unknown_type $sql
	 */
	public function update($sql=""){
		if(empty($sql)) return false;
		if(empty($this->CONN))return false;
		try {
			$results = mysql_query($sql,$this->CONN);
		} catch (Exception $e) {
			$msg = $e;
			error_log("update-error-msg :".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		
		return $results;
	}
	
	/**
	 * 数据库 删除方法
	 * Enter description here ...
	 * @param unknown_type $sql
	 */
	public function delete($sql=""){
		if(empty($sql)) return false;
		if(empty($this->CONN))return false;
		try {
			$results = mysql_query($sql,$this->CONN);
		} catch (Exception $e) {
			$msg = $e;
			error_log("delete-error-msg :".$msg."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
			include(ERRFILE);
		}
		
		return $results;
	}
	
	/**
	 * 定义事务
	 * Enter description here ...
	 */
	public function begintransaction(){
		mysql_query("SET AUTOCOMMIT = 0");
		mysql_query("BEGIN");
	}
	
	/**
	 * 回滚
	 * Enter description here ...
	 */
	public function rollback(){
		mysql_query("ROLLBACK");
	}
	
	/**
	 * 提交
	 * Enter description here ...
	 */
	public function commit(){
		mysql_query("COMMIT");
	}
	
	
}